package protocol.transactions;

import org.json.JSONObject;

import protocol.XoneProtocol;
import protocol.XoneServiceCode;
import android.text.TextUtils;

import com.google.gson.JsonElement;
import com.netease.http.THttpRequest;
import com.netease.log.NTLog;

public class GetIdKeyTransaction extends XoneBaseTransaction {

	public GetIdKeyTransaction() {
		super(TRANSACTION_TYPE_GETIDKEY);
	}
	@Override
	protected void onXoneTransactionSuccess(int code, Object obj) {
		if(obj !=null && obj instanceof JsonElement) {
		    JsonElement json = (JsonElement) obj;
			String initId = json.getAsJsonObject().get("initId").getAsString();
			String encryptedKey = json.getAsJsonObject().get("encryptedKey").getAsString();
			NTLog.i("getIdKeyTransaction", "initId is " + initId + " encryptedKey is " + encryptedKey);
			if(!TextUtils.isEmpty(initId)){
				XoneProtocol.getInstance().setIntitId(initId);
			}
			if(!TextUtils.isEmpty(encryptedKey)){
				XoneProtocol.getInstance().setEncryptedKey(encryptedKey);
			}
			notifyMessage(XoneServiceCode.TRANSACTION_SUCCESS,null);
		}
	}
	@Override
	public void onTransact() {
		THttpRequest request = XoneProtocol.getInstance().createGegIdKeyRequest();
		sendRequest(request);

	}

}
